package controller;

import databaseLayer.CustomerUpdate;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
import userBean.RegisteredCustomer;
import userBean.TicketingManager;
import userBean.TicketingOfficer;
import userBean.UserIdentity;

/**
 *
 * @author Sam
 */
public class LogoutListener implements HttpSessionAttributeListener {
    public void attributeAdded(HttpSessionBindingEvent event) {}

    public void attributeRemoved(HttpSessionBindingEvent event) {
        if (event.getName() != null && event.getName().equals("userIdentity")) {
            String userID = "";

            UserIdentity userIdentity = (UserIdentity)event.getValue();
            if (userIdentity.getUserType().equals("RegisteredCustomer")) {
                userID = ((RegisteredCustomer)userIdentity).getId();
            }
            else if (userIdentity.getUserType().equals("TicketingOfficer")) {
                userID = ((TicketingOfficer)userIdentity).getId();
            }
            else if (userIdentity.getUserType().equals("TicketingManager")) {
                userID = ((TicketingManager)userIdentity).getId();
            }

            if (!userIdentity.getUserType().equals("Visitor")) {
                CustomerUpdate.userLogout(userID);
            }
        }
    }

    public void attributeReplaced(HttpSessionBindingEvent event) {}
}
